package com.caigou.modules.center.ui.message

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.center.R
import com.caigou.modules.center.adapter.MessageItemAdapter
import com.caigou.modules.center.bean.MessageItemBean
import com.caigou.modules.center.databinding.ActivityMessageBoxBinding
import com.caigou.modules.center.viewmodel.MessageViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/15
 */
@Route(path = RouterPath.Center.PAGE_MESSAGE)
@AndroidEntryPoint
class MessageBoxActivity : BaseActivity() {

    lateinit var binding: ActivityMessageBoxBinding

    private val messageViewModel: MessageViewModel by viewModels()

    private val mAdapter: MessageItemAdapter by lazy {
        MessageItemAdapter(mutableListOf())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMessageBoxBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.messageBoxLlContainer.id
        initView()
    }

    private fun initView() {
        binding.messageBoxSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                messageViewModel.getMessageList()
            }
        }

        binding.messageBoxRecyclerView.run {
            layoutManager = LinearLayoutManager(this@MessageBoxActivity)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@MessageBoxActivity))
        }

        binding.messageBoxIvRefresh.setOnClickListener {
            messageViewModel.getMessageList()
        }

        binding.messageBoxTvCs.setOnClickListener {
            ARouter.getInstance().build(RouterPath.Center.PAGE_GM)
                .navigation()
        }

        binding.gmBoxTvBack.setOnClickListener {
            finish()
        }

        messageViewModel.getMessageList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(messageViewModel.messageBoxListLiveData, ::handleGetMessageList)
    }

    private fun handleGetMessageList(result: Resource<MutableList<MessageItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.messageBoxSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取站内消息录异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.messageBoxSwipeRefreshLayout.isRefreshing = false
                mAdapter.loadMoreModule.isEnableLoadMore = true

                if (result.data == null) {
                    return
                }
                mAdapter.setList(result.data!!)
                binding.messageBoxRecyclerView.scrollToPosition(mAdapter.itemCount - 1)
            }
        }
    }
}